package com.avalon.file.filter;

import com.avalon.core.context.Context;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;


@WebFilter(filterName = "fileFilter", urlPatterns = "/*")
public class FileFilter implements Filter {

    @Autowired
    private Context context;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {

        if (context.getApplicationConfig().getDebug()) {
            context.init(context.getApplicationConfig().getDataSource().getDatabase());
        } else {
            context.init(context.getApplicationConfig().getDataSource().getDatabase());
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }
}